package com.itchen.homework;

import com.itchen.pojo.User;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @author げっしょく
 * @date 2021/8/29
 */


@WebServlet("/home2")
public class homework2 extends HttpServlet {
    private ArrayList<User> lis = new ArrayList<User>();
    /*
    2. 模拟注册
        register.html
            form
                用户名  文本输入框
                姓名    文本输入框
                性别    单选框
                生日    日期
                爱好    复选框
                籍贯    下拉列表  （随便整几个省份就行）
        RegisterServlet
            获取注册提交过来的数据，将其封装成User对象。
     */

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = new User();
        String username = req.getParameter("username");
        //System.out.println(username);
        user.setUsername(username);

        String name = req.getParameter("name");
        byte[] bytes = name.getBytes("iso-8859-1");
        String s = new String(bytes, "UTF-8");
        //System.out.println(s);
        user.setName(s);

        String sex = req.getParameter("sex");
        if("0".equals(sex)){
            sex = "女";
        }else if("1".equals(sex)){
            sex = "男";
        }
        //System.out.println(sex);
        user.setSex(sex);


        String birthday = req.getParameter("birthday");
        //System.out.println(birthday);
        user.setBirthday(birthday);

        String[] hobby = req.getParameterValues("hobby");
        for(int i= 0; i < hobby.length; i++) {
            String s1 = hobby[i];
            if("6".equals(s1)){
                hobby[i] = "游泳";
            }else if("7".equals(s1)){
                hobby[i] = "游戏";
            } else if ("8".equals(s1)) {
                hobby[i] = "跳伞";
            }
        }
        /*for (String s1 : hobby) {
            System.out.print(s1 + "...");
        }
        System.out.println();*/
        user.setHobby(hobby);


        String site = req.getParameter("site");
        site = new String(site.getBytes("iso-8859-1"), "UTF-8");
        //System.out.println(string);
        user.setSite(site);
        System.out.println(user);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
